﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Parser
{
    /// <summary>
    /// 表示基类型DFA状态
    /// </summary>
    /// <typeparam name="TokenType">记号类型</typeparam>
    public abstract class DFAState<TokenType> : IEqualityComparer<DFAState<TokenType>>
    {
        Dictionary<char,DFAState<TokenType>> _nextState;
        int _stateId;

        public DFAState(int stateId)
        {
            _nextState = new Dictionary<char, DFAState<TokenType>>();
            _stateId = stateId;
        }

        //---------------------------------------------------------------------------------------------------
        //  DFAState的设计要点
        //  1) 状态迁移
        //  2) 状态保持
        //  3) 状态回退
        //---------------------------------------------------------------------------------------------------

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <param name="nextState"></param>
        /// <returns></returns>
        public bool AddState(char ch, DFAState<TokenType> nextState)
        {
            if (!_nextState.ContainsKey(ch)) {
                _nextState.Add(ch, nextState);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        protected DFAState<TokenType> GetNextState(char ch)
        {
            if (_nextState.ContainsKey(ch)) {
                return _nextState[ch];
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection">标记集合</param>
        /// <param name="stateStack">状态堆栈</param>
        /// <param name="stream">字符流</param>
        /// <param name="state">当前状态</param>
        /// <param name="last">上一个记号</param>
        /// <param name="now">当前正在处理的记号</param>
        /// <returns></returns>
        public abstract DFAState<TokenType> Action(
            ICollection<TokenBase<TokenType>> collection, StateStack<TokenType> stateStack, CharacterStream stream,
            DFAState<TokenType> state, ref TokenBase<TokenType> lastToken, ref TokenBase<TokenType> nowToken);

        #region Override

        public override bool Equals(object obj)
        {
            if(obj is DFAState<TokenType> state) {
                return state._stateId == this._stateId;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return this._stateId.GetHashCode() ^ base.GetHashCode();
        }

        public override string ToString()
        {
            return StateName;
        }

        #endregion

        #region IEqualityComparer<DFAState<TokenType>> 接口成员

        public bool Equals(DFAState<TokenType> x, DFAState<TokenType> y)
        {
            return x._stateId == y._stateId;
        }

        public int GetHashCode(DFAState<TokenType> obj)
        {
            return obj.GetHashCode();
        }

        #endregion

        /// <summary>
        /// 为当前DFAState提供友好的状态名
        /// </summary>
        public abstract string StateName { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual DFAState<TokenType> this[char key] {
            get {
                return GetNextState(key);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int State {
            get {
                return _stateId;
            }
        }
    }
}
